package com.example.yl.service.impl;


import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.util.GetTokenUtil;
import com.example.util.HttpClientUtils;
import com.example.util.JwdUtil;
import com.example.yl.entity.Iyqww_YL_1w1m;
import com.example.yl.entity.Iyqww_YL_1w1mtb;
import com.example.yl.entity.Iyqww_YL_ssbxt;
import com.example.yl.entity.Iyqww_YL_ssbxtVo;
import com.example.yl.mapper.YL_1w1mMapper;
import com.example.yl.mapper.YL_1w1m_tbMapper;
import com.example.yl.mapper.YL_ssbMapper;
import com.example.yl.service.YLService;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author
 * @createtime 2021-08-14 9:27
 * @description
 */
@Service
public class YLServiceImpl implements YLService {

    @Autowired
    private YL_1w1m_tbMapper yl_1w1m_tbMapper;

    @Autowired
    private YL_ssbMapper yl_ssbMapper;

    @Autowired
    private YL_1w1mMapper yl_1w1mMapper;

    @Autowired
    private GetTokenUtil getTokenUtil;


    @Override
    public ArrayList<Iyqww_YL_1w1mtb> findAddress() {
        return yl_1w1m_tbMapper.findAddress();
    }

    @Override
    public ArrayList<Iyqww_YL_ssbxt> findAddress1() {
        return yl_ssbMapper.findAddress();
    }

    @Override
    public ArrayList<Iyqww_YL_1w1m> findAddress2() {
        return yl_1w1mMapper.findAddress();
    }

    @Override
    public void addForeignId(String str, String address) {

        yl_1w1m_tbMapper.addForeignId(str, address);
    }

    @Override
    public void addForeignId1(String foreignId, String address) {

        yl_ssbMapper.addForeignId(foreignId, address);
    }

    @Override
    public void addForeignId_val(String foreignId_val, String address) {
        yl_1w1mMapper.addForeignId_val(foreignId_val, address);
        System.out.println(foreignId_val);
        System.out.println(address);

    }

    @Override
    public List<Map<String, Object>> staticData(String foreignId) {
        return yl_ssbMapper.staticData(foreignId);
    }

    @Override
    public ArrayList<Map<String, Object>> findEssential() {


        ArrayList<Map<String, Object>> list1 = new ArrayList<Map<String, Object>>();

        List<Iyqww_YL_1w1mtb> list = yl_1w1m_tbMapper.findEssential();
        //查询随手报中的foreignId
        ArrayList<Iyqww_YL_ssbxt> ssb_list = yl_ssbMapper.findAddress();
        // regEEx-- String规则
        String regEx = "[\n`~!@#$%^&*()+=|';'\\[\\].<>/?~！@#￥%……&*（）——+|【】‘；：”“’。 、？]";
        // 把规则编译成模式对象
        Pattern p = Pattern.compile(regEx);
        String deptName = null;
        String userName = null;
        String mobile = null;

        String aa = "";
        // url 根据用户id获取用户信息 云枢接口
        String url = "http://acl.iansn.com/api/api/organization/user/info";
        // dept_url 根据部门id获取部门信息 云枢接口
        String dept_url = "http://acl.iansn.com/api/api/organization/department/get_dept_info";
        try {
            String token = getTokenUtil.getToken();

            for (Iyqww_YL_1w1mtb iy : list) {

                //点位基础信息pointInformationMap
                Map<String, Object> pointInformationMap = new HashMap<>();
                //获取地址
                String address = iy.getAddress();
                //将地址转为经纬度
                String addressStr = JwdUtil.getLonLat1(address);
//                System.out.println(addressStr);
                //获取1w1m_tb的foreignId
                String foreignId = iy.getForeignId();

                //获取部门id
                String StaffSelector1608775420787 = iy.getStaffSelector1608775420787();
                if (StaffSelector1608775420787 != null && StaffSelector1608775420787 != "") {
                    // 通过模式对象得到匹配器对象
                    Matcher ma = p.matcher(StaffSelector1608775420787);
                    String newStr = ma.replaceAll(aa).trim();
                    //将处理后的字符串放到map中
                    Map map = (Map) JSON.parse(newStr);
                    //从map中取得id的值
                    String deptId = (String) map.get("id");
                    //根据部门ID获取部门信息
                    String dept = HttpClientUtils.doGetDept(dept_url, token, deptId);
                    //处理得到的部门信息
                    JSONObject deptJson = JSONObject.fromObject(dept);
                    //从部门信息中获取data的值
                    String dataStr = deptJson.getString("data");
                    //处理得到的data值
                    JSONObject deptJson1 = JSONObject.fromObject(dataStr);
                    //从data值中获取到部门名称deptName
                    deptName = deptJson1.getString("name");
                }

//                System.out.println("deptName============" + deptName);

                //获取用户id
                String staffSelector1608775349969 = iy.getStaffSelector1608775349969();
                if (staffSelector1608775349969 != null && staffSelector1608775349969 != "") {
                    Matcher m = p.matcher(staffSelector1608775349969);
                    String newString = m.replaceAll(aa).trim();
                    Map maps = (Map) JSON.parse(newString);
                    String userId = (String) maps.get("id");
                    //根据用户ID获取姓名、电话
                    String inforSt = HttpClientUtils.doGetUser(url, token, userId);
                    JSONObject userJson = JSONObject.fromObject(inforSt);
                    String user_data = userJson.getString("data");
                    JSONObject userJson1 = JSONObject.fromObject(user_data);
                    userName = userJson1.getString("name");
                    mobile = userJson1.getString("mobile");
                }
//                System.out.println("userName============" + userName);
//                System.out.println("mobile============" + mobile);

                pointInformationMap.put("userName", userName);
                pointInformationMap.put("mobile", mobile);
                pointInformationMap.put("address", address);
                pointInformationMap.put("addressJwd", addressStr);
                pointInformationMap.put("foreignId", foreignId);
                pointInformationMap.put("deptName", deptName);
                //循环ssb_list得到ssbForeignId
                for (Iyqww_YL_ssbxt iy_ssb : ssb_list) {
                    String ssbForeignId = iy_ssb.getForeignId();

                    //判断随手报的foreignId是否与1w1m_tb中的foreignId相同
                    if (ssbForeignId != null && ssbForeignId != "" && ssbForeignId.equals(foreignId)) {
                        //相同则查询办结情况
                        List<Map<String, Object>> ssbList = yl_ssbMapper.staticData(foreignId);

                        for (Map<String, Object> map1 : ssbList) {

                            if (map1.get("ShortText1608803337156").equals("已办结")) {
                                Object num = map1.get("num");

                                pointInformationMap.put("completed", num);
//                                System.out.println("已办结数量==="+num);
                            } else {
                                Object num = map1.get("num");
                                pointInformationMap.put("processing", num);
                            }

                        }
                    } else {//随手报的foreignId是否与1w1m_tb中的foreignId不相同时直接赋值

                        pointInformationMap.put("completed", 0);
                        pointInformationMap.put("processing", 0);
                    }

                }

                System.out.println("tb_1w1mMap====" + pointInformationMap);
                list1.add(pointInformationMap);

            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        return list1;
    }


    @Override
    public List<Iyqww_YL_ssbxtVo> queryAllSsbVo(Page<Iyqww_YL_ssbxtVo> pagination, String foreignId) {
        //查数据库获取数据
        List<Iyqww_YL_ssbxtVo> ssbxtLists = yl_ssbMapper.queryAllSsbVo(pagination, foreignId);

        List<Iyqww_YL_ssbxtVo> ssbxtList = new ArrayList<>();


        for (Iyqww_YL_ssbxtVo ssbxtVo : ssbxtLists) {

            Iyqww_YL_ssbxtVo ssbxtVo1 = new Iyqww_YL_ssbxtVo();

            String problemsSummary = "";

            String str1 = "";


            String shortText1608777161506 = ssbxtVo.getShortText1608777161506();
            String shortText1608796211477 = ssbxtVo.getShortText1608796211477();
            String shortText1608799766004 = ssbxtVo.getShortText1608799766004();
            String shortText1608801262580 = ssbxtVo.getShortText1608801262580();
            String shortText1608801569614 = ssbxtVo.getShortText1608801569614();
            String shortText1608801859045 = ssbxtVo.getShortText1608801859045();

            String str = shortText1608777161506 + "," + shortText1608796211477 + "," +
                    shortText1608799766004 + "," + shortText1608801262580 + "," +
                    shortText1608801569614 + "," + shortText1608801859045;

            str1 = StrUtil.removeAll(str, "null,");

            if (str1.endsWith(",")) {
                //字符串如果以“，”结尾则去掉字符的最后一个字符
                problemsSummary = str1.substring(0, str1.length() - 1);

                System.out.println("sfsfsfklafks" + problemsSummary);

            } else if (str1.endsWith(",null")) {
                problemsSummary = StrUtil.removeAll(str1, ",null");
            } else {
                problemsSummary = str1;
            }

            ssbxtVo1.setProblemsSummary(problemsSummary);
            ssbxtVo1.setActivityName(ssbxtVo.getActivityName());
            ssbxtVo1.setAddress(ssbxtVo.getAddress());
            ssbxtVo1.setForeignId(ssbxtVo.getForeignId());
            ssbxtVo1.setSequenceStatus(ssbxtVo.getSequenceStatus());
            ssbxtVo1.setShortText1608803337156(ssbxtVo.getShortText1608803337156());
            ssbxtVo1.setParticipantName(ssbxtVo.getParticipantName());
            ssbxtVo1.setReceiveTime(ssbxtVo.getReceiveTime());
            ssbxtVo1.setShortText1608772055540(ssbxtVo.getShortText1608772055540());
            ssbxtVo1.setShortText1608772160647(ssbxtVo.getShortText1608772160647());
            ssbxtList.add(ssbxtVo1);


        }


        return ssbxtList;
    }

    @Override
    public List<Iyqww_YL_ssbxtVo> queryAllSsbVo1(Page<Iyqww_YL_ssbxtVo> pagination, String foreignId) {
        //查数据库获取数据
        List<Iyqww_YL_ssbxtVo> ssbxtLists = yl_ssbMapper.queryAllSsbVo1(pagination, foreignId);

        List<Iyqww_YL_ssbxtVo> ssbxtList = new ArrayList<>();


        for (Iyqww_YL_ssbxtVo ssbxtVo : ssbxtLists) {

            Iyqww_YL_ssbxtVo ssbxtVo1 = new Iyqww_YL_ssbxtVo();

            String problemsSummary = "";

            String str1 = "";


            String shortText1608777161506 = ssbxtVo.getShortText1608777161506();
            String shortText1608796211477 = ssbxtVo.getShortText1608796211477();
            String shortText1608799766004 = ssbxtVo.getShortText1608799766004();
            String shortText1608801262580 = ssbxtVo.getShortText1608801262580();
            String shortText1608801569614 = ssbxtVo.getShortText1608801569614();
            String shortText1608801859045 = ssbxtVo.getShortText1608801859045();

            String str = shortText1608777161506 + "," + shortText1608796211477 + "," +
                    shortText1608799766004 + "," + shortText1608801262580 + "," +
                    shortText1608801569614 + "," + shortText1608801859045;

            str1 = StrUtil.removeAll(str, "null,");

            if (str1.endsWith(",")) {
                //字符串如果以“，”结尾则去掉字符的最后一个字符
                problemsSummary = str1.substring(0, str1.length() - 1);

                System.out.println("sfsfsfklafks" + problemsSummary);

            } else if (str1.endsWith(",null")) {
                problemsSummary = StrUtil.removeAll(str1, ",null");
            } else {
                problemsSummary = str1;
            }

            ssbxtVo1.setProblemsSummary(problemsSummary);
            ssbxtVo1.setActivityName(ssbxtVo.getActivityName());
            ssbxtVo1.setAddress(ssbxtVo.getAddress());
            ssbxtVo1.setForeignId(ssbxtVo.getForeignId());
            ssbxtVo1.setSequenceStatus(ssbxtVo.getSequenceStatus());
            ssbxtVo1.setShortText1608803337156(ssbxtVo.getShortText1608803337156());
            ssbxtVo1.setParticipantName(ssbxtVo.getParticipantName());
            ssbxtVo1.setReceiveTime(ssbxtVo.getReceiveTime());
            ssbxtVo1.setShortText1608772055540(ssbxtVo.getShortText1608772055540());
            ssbxtVo1.setShortText1608772160647(ssbxtVo.getShortText1608772160647());
            ssbxtList.add(ssbxtVo1);


        }


        return ssbxtList;
    }

}
